<?php

class AdminUserController extends Controller
{
    protected function _checkUserLevel()
    {
        /*
        echo '<pre>';
        var_dump($_SESSION);
        echo '</pre>';
        */

        if (!isset($_SESSION['user_data']['user_level_name']) || $_SESSION['user_data']['user_level_name'] != 'Administrator')
        {
            InputFilter::purgeAll();
            $controller = new AdminLoginController();
            $controller->login();
            return false;
        }

        return true;
    }

    public function defaultAction()
    {
        $this->listUsers();
    }

    public function listUsers()
    {
        if (!$this->_checkUserLevel()) return;

        $users = UserModel::getAllUsers();
        $this->_toOutput('users', $users);

        $this->_display('admin/users', 'list');
    }

    public function edit()
    {
        if (!$this->_checkUserLevel()) return;

        $user_id = InputFilter::fetch('id', 'get');

        $user_data = UserModel::getUserData($user_id);
        foreach ($user_data as $key => $val)
        {
            if ($key != 'password')
            $this->_toOutput($key, $val);
        }

        $possible_user_levels = UserModel::getUserLevels();
        $this->_toOutput('possible_user_levels', $possible_user_levels);

        $this->_display('admin/users', 'edit');
    }

    public function save()
    {
        if (!$this->_checkUserLevel()) return;

        $possible_user_levels = UserModel::getUserLevels();
        $this->_toOutput('possible_user_levels', $possible_user_levels);

        $user_data = array();

        $user_data['id'] = InputFilter::fetch('id');
        $this->_toOutput('id', $user_data['id']);

        $user_data['email'] = InputFilter::fetch('admin_email');
        $this->_toOutput('email', $user_data['email']);

        $user_data['password'] = InputFilter::fetch('admin_password');
        $user_data['password_check'] = InputFilter::fetch('admin_password_check');

        $user_data['first_name'] = InputFilter::fetch('first_name');
        $this->_toOutput('first_name', $user_data['first_name']);

        $user_data['last_name'] = InputFilter::fetch('last_name');
        $this->_toOutput('last_name', $user_data['last_name']);

        $user_data['user_level'] = InputFilter::fetch('user_level');
        $this->_toOutput('user_level', $user_data['user_level']);

        $user_data['on_line'] = InputFilter::fetch('on_line');
        $this->_toOutput('on_line', $user_data['on_line']);

        $errors = array();

        if (empty($user_data['email']))
        {
            $errors[] = 'Email je potrebno upisati.';
        }

        $mail_format_check = preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $user_data['email']);
        if (!empty($user_data['email']) && empty($mail_format_check))
        {
            $errors[] = 'Email nije pravilan.';
        }
        if (empty($user_data['password']) && $user_data['id'] == 0)
        {
            $errors[] = 'Zaporka mora biti određena.';
        }
        if ($user_data['password'] != $user_data['password_check'])
        {
            $errors[] = 'Niste dobro ponovili zaporku.';
        }
        if (empty($user_data['first_name']))
        {
            $errors[] = 'Ime je potrebno upisati.';
        }
        if (empty($user_data['last_name']))
        {
            $errors[] = 'Prezime je potrebno upisati.';
        }
        if (empty($user_data['user_level']))
        {
            $errors[] = 'Vrsta korisnika mora biti odabrana.';
        }

        if (!empty($errors))
        {
            $this->_toOutput('errors', $errors);
            $this->_display('admin/users', 'edit');
            return;
        }

        if ($user_data['id'] == 0)
        {
            $success = UserModel::insertUser($user_data);
        }
        else
        {
            if (!empty($user_data['password']))
            {
                $with_password = true;
            }
            else
            {
                $with_password = false;
            }

            $success = UserModel::updateUser($user_data, $with_password);
        }

        if ($success && $user_data['id'] == 0)
        {
            $this->_toOutput('success', 'Korisnik uspješno upisan.');
            $this->_toOutput('url', '/admin/user/new');
            $this->_display('admin', 'message');
            return;
        }
        elseif ($success && $user_data['id'] > 0)
        {
            $this->_toOutput('success', 'Korisnikovi podaci uspješno spremljeni.');
            $this->_toOutput('url', '/admin/user/edit?id=' . $user_data['id']);
            $this->_display('admin', 'message');
            return;
        }

        $this->_toOutput('errors', array('Upis nije uspio, pokušate kasnije.'));
        $this->_display('admin/users', 'edit');
    }
}

?>